﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FHost1
{
    public partial class AdminMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies.AllKeys.Contains("fhostadmin"))
            {
                pnlAccount.Visible = true;
                pnlLogin.Visible = false;
            }
            else
            {
                pnlAccount.Visible = false;
                pnlLogin.Visible = true;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                using (FHostDataContext dataContext = new FHostDataContext())
                {
                    string adminUser = System.Configuration.ConfigurationManager.AppSettings["AdminUser"];
                    string adminPass = System.Configuration.ConfigurationManager.AppSettings["AdminPass"];
                    if (txtLoginUsername.Text.Trim() == adminUser && txtLoginPassword.Text == adminPass)
                    {
                        string cookie = "admin";
                        Response.Cookies.Add(new HttpCookie("fhostadmin", cookie));
                        Response.Cookies["fhostadmin"].Expires = DateTime.Now.AddHours(1);
                        Response.Redirect("Uploads.aspx");
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Invalid username and password!');", true);
                    }
                }
            }
            catch
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Error while login. Please try again after some time!');", true);
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Response.Cookies["fhostadmin"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect("Default.aspx");
        }
    }
}